<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="输入" prop="inputJson">
        <el-input v-model="formData.inputJson" type="textarea" placeholder="请输入需要美化格式Json输入" show-word-limit
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="美化" prop="field103">
        <el-button type="primary" icon="el-icon-search" size="medium" @click="formatJson(formData.inputJson)"> 主要按钮 </el-button>
      </el-form-item>
      <el-form-item label="输出" prop="field104">
        <el-input v-model="formData.field104" type="textarea" readonly :autosize="{minRows: 4, maxRows: 10}"
          :style="{width: '100%'}"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {format} from "@/api/system/json";

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        inputJson: undefined,
        field103: undefined,
        field104: undefined,
      },
      rules: {
        inputJson: [{
          required: true,
          message: '请输入需要美化格式Json输入',
          trigger: 'blur'
        }],
        field104: [],
      },
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    /** 格式化Json */
    formatJson(inputJson) {
      if (!inputJson) {
        this.$message.error('请输入需要美化格式Json输入')
        return
      }
      try {
        console.log(inputJson);
        format(inputJson).then(res => {
          this.formData.field104 = JSON.stringify(res, null, 2)
        })
      } catch (e) {
        this.$message.error('Json格式错误')
      }
    }
  }
}

</script>
<style>
</style>
